package com.tool_stdy.thread;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;

public class ProdrceConsumer {

	private static final ArrayBlockingQueue<String> fileValue = new ArrayBlockingQueue<String>(
			10000);
	private static final ArrayBlockingQueue<HashMap<String, String>> sortValue = new ArrayBlockingQueue<HashMap<String, String>>(
			10000);
	private static final ConcurrentHashMap<String, String> parames = new ConcurrentHashMap<String, String>();
	static final ExecutorService pool = Executors.newFixedThreadPool(7);
	static final CountDownLatch count = new CountDownLatch(3);


	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 1; i++) {
			Future<String> submit = pool.submit(new Callable<String>() {
				public String call() throws Exception {
					getValueFromFile();
					return "suc";
				}
			});
		}

		for (int i = 0; i < 3; i++) {
			pool.submit(new Callable<String>() {
				public String call() throws Exception {
					sortValueByMap();
					return "suc";
				}
			});
		}

		for (int i = 0; i < 3; i++) {
			pool.submit(new Callable<String>() {
				public String call() throws Exception {
					saveMaps();
					count.countDown();
					return "suc";
				}
			});
			
		}
		count.await();

		writeFile();
	}

	public static void getValueFromFile() throws Exception {
		try {
			List<String> readLines = FileUtils.readLines(new File("E:/target"),
					"utf-8");
			for (String src : readLines) {
				if (src.contains("https")) {
					Thread.sleep(3000);
				}
				fileValue.put(src);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void sortValueByMap() throws Exception {
		for (int i = 0; i < 10000; i++) {
			HashMap<String, String> maps = new HashMap<String, String>();
			String getValue = fileValue.poll(5, TimeUnit.SECONDS);
			String[] split = getValue.split(",");
			maps.put(split[0], split[1]);
			sortValue.add(maps);
			System.out.println(Thread.currentThread().getName() + "-->" + maps);
		}
	}

	public static void saveMaps() throws Exception {
		for (int i = 0; i < 10000; i++) {
			HashMap<String, String> getValue = sortValue.poll(5,
					TimeUnit.SECONDS);
			parames.putAll(getValue);
			System.out.println(Thread.currentThread().getName() + "-->"
					+ getValue);
		}
	}

	public static void writeFile() throws IOException {
		StringBuffer sb = new StringBuffer();
		HashMap<Integer, String> map = new HashMap<Integer, String>();
		for (Entry<String, String> entry : parames.entrySet()) {
			map.put(Integer.valueOf(entry.getKey()), entry.getValue());
		}
		for (Entry<Integer, String> entry : map.entrySet()) {
			sb.append(entry.getKey()).append("--->").append(entry.getValue())
					.append("\n");
		}
		System.out.println("存储成功..");
		FileUtils.writeStringToFile(new File("E:/target2"), sb.toString());
	}
}
